class LoadingUI extends egret.Sprite implements RES.PromiseTaskReporter {

    public constructor() {
        super();
        this.once(egret.Event.ADDED_TO_STAGE,this.onAddToStage,this);
    }

    private label = new egret.TextField;
    private bar: ProgressBar; 
 
    private onAddToStage(): void {
        this.graphics.beginFill(0x111111,0.8);
        this.graphics.drawRect(0,0,this.stage.stageWidth,this.stage.stageHeight);
        this.graphics.endFill();

        let label = this.label;
        label.size = 64;
        label.y = 300;
        label.width = 480;
        label.height = 100;
        label.textAlign = "center";
        this.placeCenter(label,0,-60);
 

        this.bar = new ProgressBar; 
        let bar = this.bar; 
        this.placeCenter(bar); 
    }

    public onProgress(current: number, total: number): void {
        this.label.text = `Loading...${current}/${total}`;
        this.bar.value = 100 * current / total;
    }

    private placeCenter(obj: egret.DisplayObject, x_offset: number = 0, y_offset: number = 0) {
        let sw = this.stage.stageWidth;
        let sh = this.stage.stageHeight;

        obj.x = (sw - obj.width) / 2 + x_offset;
        obj.y = (sh - obj.height) / 2 + y_offset;

        this.addChild(obj);
    }
}
